import type { Effect, Reducer } from 'umi';
import { queryCardList } from './service';

import type { CardListItem } from './data';

export interface StateType {
  list: CardListItem[]
}

export interface ModelType {
  namespace: string;
  state: StateType;
  effects: {
    fetch: Effect
  }
  reducers: {
    queryCardList: Reducer<StateType>
  }
}

const Model: ModelType = {
  namespace: 'cardlistmodel',

  state: {
    list: []
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(queryCardList, payload);
      yield put({
        type: 'queryCardList',
        payload: Array.isArray(response) ? response : []
      })
    }
  },

  reducers: {
    queryCardList(state, action) {
      return {
        ...state,
        list: action.payload
      }
    }
  }
}

export default Model;